package sk.tuke.client.view;

import sk.tuke.client.ApplicationClickHandler;
import sk.tuke.client.interfaces.LayoutFactory;

import com.google.gwt.user.client.ui.Button;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.data.RestDataSource;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.types.FieldType;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.ImgButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class HealthCardView extends VLayout {
	
	private Integer healthcardNumber;
	
	public HealthCardView(Integer healthcardNumber) {
		super();
		this.healthcardNumber = healthcardNumber;

		RestDataSource medicalRecordsDatasource = new RestDataSource();  
        medicalRecordsDatasource.setDataURL("http://localhost:8080/healthCard/healthcardmanagement/gethealthcard");  
        medicalRecordsDatasource.setRecordXPath("GetHealthCardOutput/healthCard/medicalRecords");  
        medicalRecordsDatasource.setCallbackParam("GetHealthCardOutput");
        medicalRecordsDatasource.setDataFormat(DSDataFormat.JSON);
        
        DataSourceField number = new DataSourceField("number", FieldType.INTEGER, "Number");  
        DataSourceField date = new DataSourceField("date", FieldType.DATE, "Date");  
        DataSourceField diseases = new DataSourceField("diseases", FieldType.TEXT, "Drug");
        DataSourceField symptoms = new DataSourceField("symptoms", FieldType.TEXT, "Drug");
        DataSourceField drugs = new DataSourceField("drugs", FieldType.TEXT, "Drug");
        DataSourceField status = new DataSourceField("status", FieldType.TEXT, "Status");
        medicalRecordsDatasource.setFields(number, date, diseases, symptoms, drugs, status); 
        
        final ListGrid healthCardGrid = new ListGrid() {  
            @Override  
            protected Canvas createRecordComponent(final ListGridRecord record, Integer colNum) {  
  
                String fieldName = this.getFieldName(colNum);  
  
                if (fieldName.equals("menuField")) {  
                    HLayout recordCanvas = new HLayout(3);  
                    recordCanvas.setHeight(22);    
                 
                    ImgButton openImg = new ImgButton();
                    openImg.setShowDown(false);  
                    openImg.setShowRollOver(false);   
                    openImg.setSrc("open.png");  
                    openImg.setPrompt("Open Record");  
                    openImg.setHeight(16);  
                    openImg.setWidth(16);
                    openImg.addClickHandler(new ApplicationClickHandler(){
            			@Override
            			public void onClick(ClickEvent event) {
            				MedicalRecordView.Factory factory = new MedicalRecordView.Factory();
            				Canvas view = factory.create(record.getAttributeAsInt("number"));
            				southLayout.setMembers(view, westLayout);
            			}
                    });
  
                    ImgButton editImg = new ImgButton();  
                    editImg.setShowDown(false);  
                    editImg.setShowRollOver(false);  
                    editImg.setSrc("edit.png");  
                    editImg.setPrompt("Edit Record");  
                    editImg.setHeight(16);  
                    editImg.setWidth(16);  
                    editImg.addClickHandler(new ApplicationClickHandler(){
            			@Override
            			public void onClick(ClickEvent event) {
            				SC.say("Not implemented");
            			}
                    });
                    
                    recordCanvas.addMember(openImg);  
                    recordCanvas.addMember(editImg);
                    return recordCanvas;  
                } else {  
                    return null;  
                }  
  
            }  
        };
        healthCardGrid.setShowRecordComponents(true);          
        healthCardGrid.setShowRecordComponentsByCell(true);   
        healthCardGrid.setShowFilterEditor(true);  
        healthCardGrid.setFilterOnKeypress(true);
        healthCardGrid.setHeight(400);
        healthCardGrid.setDataSource(medicalRecordsDatasource);    
        healthCardGrid.setShowAllRecords(true);
        
        ListGridField numberField = new ListGridField("number", 60);
        numberField.setCanFilter(false);
        ListGridField diseaseField = new ListGridField("diseases", "Disease");
        diseaseField.setDataPath("/diagnose/disease/name");
//        ListGridField symptomsField = new ListGridField("symptoms", "Symptoms");
//        symptomsField.setDataPath("/diagnose/symptoms/name");
//        ListGridField drugsField = new ListGridField("drugs", "Drugs");
//        drugsField.setDataPath("/diagnose/drugs/name");
        ListGridField dateField = new ListGridField("date", "Date");
        ListGridField statusField = new ListGridField("status", "Status");
        ListGridField menuField = new ListGridField("menuField", "Menu", 60);
        menuField.setCanFilter(false);
        healthCardGrid.setFields(numberField, diseaseField, dateField, statusField, menuField);
        healthCardGrid.fetchData(new Criteria("number", String.valueOf(healthcardNumber)));
        
        Button newHealthcardButton = new Button("<span style='font-size: 20px'><b>+</b></span> New medical record");
        newHealthcardButton.setWidth("200");
        
   		// initialise the Accounts View layout container
		this.addStyleName("crm-ContextArea");
		this.setWidth("*");

		// add the List Grid to the Accounts View layout container
		VLayout a = new VLayout();
		a.setPadding(30);
		a.addMember(newHealthcardButton);
		a.addMember(healthCardGrid);
		this.addMember(a);
		
	}

	public static class Factory implements LayoutFactory {

		public Canvas create() {
			return new HealthCardView(null);
		}
		
		public Canvas create(Integer healthcardNumber) {
			return new HealthCardView(healthcardNumber);
		}

	}

}
